<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
//  这个文件是 JCAT PHP框架的一部，该项目和此文件 均遵循 GNU 自由软件协议
// 
//  Copyleft 2008 JeCat.cn(http://team.JeCat.cn)
//
//
//  JCAT PHP框架 的正式全名是：Jellicle Cat PHP Framework。
//  “Jellicle Cat”出自 Andrew Lloyd Webber的音乐剧《猫》（《Prologue:Jellicle Songs for Jellicle Cats》）。
//  JCAT 是一个开源项目，它像音乐剧中的猫一样自由，你可以毫无顾忌地使用JCAT PHP框架。JCAT 由中国团队开发维护。
//  正在使用的这个版本是：0.5.0 / SVN信息: $Id: class.JCAT_DBAggregationModel.php 1603 2009-03-31 15:35:42Z alee $
//
//
//
//  相关的链接：
//    [主页] http://jcat.JeCat.cn
//    [下载(HTTP)] http://code.google.com/p/jcat-php/downloads/list
//    [下载(svn)] svn checkout http://jcat-php.googlecode.com/svn/branches/0.4.0/Framework/ JCAT0.4
//    [在线文档] http://jcat.JeCat.cn/document
//    [社区] http://jj.jecat.cn/forum-7-1.html
//  不很相关：
//    [MP3] http://www.google.com/search?q=jellicle+songs+for+jellicle+cats+Andrew+Lloyd+Webber
//    [VCD/DVD] http://www.google.com/search?q=CAT+Andrew+Lloyd+Webber+video
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
/*-- Project Introduce --*/
namespace JCAT ;






/**
 * What's this Class ?
 *
 * @author		alee
 * @access		public
 */
class DBAggregationModel extends AggregationModel implements IDBAggregationModel
{
	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$arrORMConfig=array()	array, ORM
	 * @param	$aDB=null					DB
	 * @return	void
	 */
	public function DBAggregationModel( $ORMOrConfig=array(), DB $aDB=null )
	{
		// call parent construct
		$this->AggregationModel() ;
		
		if( is_array($ORMOrConfig) )
		{
			// orm
			$aORM = new ORM($aDB) ;
			$aORM->InitFromConfig($ORMOrConfig) ;
			$this->SetORM($aORM) ;
		}
		
		else if (GlobalFunc::IsKindOf($ORMOrConfig,'ORM') )
		{
			$this->SetORM($ORMOrConfig) ;
		}
		
		else
		{
			throw new Exception(Language::SentenceEx(
				'参数 $ORMOrConfig 必须为 ORM Config 数组 或 ORM 对象'
				, 'JCAT', null
			)) ;
		}
	}
	
	/**
	 * create a db aggregation model by passing a ORM
	 *
	 * @access	public
	 * @param	$aORM			ORM
	 * @static
	 * @return	DBAggregationModel
	 */
	static public function CreateByORM(ORM $aORM)
	{		
		return new self($aORM) ;
	}
	
	
	/**
	 * get db primary key
	 *
	 * @access	public
	 * @return	string
	 */
	public function GetPrimaryKey()
	{
		$aORM = $this->GetORM() ;
		ASSERT::ASSERT_NOTNULL($aORM,Language::SentenceEx('no ORM ?','JCAT',null))  ;
		
		return $aORM->GetPrimaryKey() ;
	}
	
	
	/**
	 * get the primary key value
	 *
	 * @access	public
	 * @return	string
	 */
	public function GetId()
	{
		$aModel = $this->CurrentModel() ;
		return $aModel? $aModel->GetId(): null ;
	}


	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$sFieldName	string	What's this Parameter ?
	 * @return	
	 */
	public function GetByField( $sFieldName )
	{
		$aModel = $this->CurrentModel() ;
		return $aModel? $aModel->GetByField(): null ;
	}
	
	/**
	 * create a db aggregation model from a prototype db model
	 *
	 * @access	public
	 * @param	$aPrototype			IDBModel
	 * @static
	 * @return	DBAggregationModel
	 */
	static public function CreateFromPrototype(IDBModel $aPrototype)
	{
		$aAggModel = self::CreateByORM($aPrototype->GetORM()) ;
		$aAggModel->SetPrototype($aPrototype) ;
		
		return $aAggModel ;
	}
	
	
	
	/**
	 * Description
	 *
	 * @access	public
	 * @param	$Prototype		null,string,IDBModel
	 * @return	void
	 */
	public function SetPrototype($Prototype)
	{
		// prototype
		ASSERT::ASSERT_ISTHESE( $Prototype, array('null','JCAT\IDBModel')
					, Language::SentenceEx('参数 $Prototype 必须是一个实现IDBModel接口的 类名或实例。','JCAT',null) );
		$this->Prototype = $Prototype ;
	}
	
	/**
	 * Description
	 *
	 * @access	protected
	 * @return	IDBModel
	 */
	protected function CreateChildModelByPrototype()
	{
		// create a DBModel instance
		if( $this->Prototype==null )
		{
			return DBModel::CreateByORM($this->GetORM()) ;
		}
		
		// create a special instance
		else if( is_string($this->Prototype) )
		{
			return call_user_func_array( array($this->Prototype,'CreateByORM'), array($this->GetORM()) ) ;
		}
		
		// clone a instance from prototype
		else if( is_object($this->Prototype) )
		{
			return clone $this->Prototype ;
		}
		
		else
		{
			ASSERT::ASSERT_(0,'!?') ;
		}
	}

	/**
	 * set ORM
	 *
	 * @access	public
	 * @param 	$aORM		ORM
	 * @return	void
	 */
	public function SetORM(ORM $aORM)
	{
		$this->aORM = $aORM ;
	}
	
	/**
	 * get ORM
	 *
	 * @access	public
	 * @return	ORM
	 */
	public function GetORM()
	{
		return $this->aORM ;
	}

	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function __sleep()
	{
		$arrSavePropertys = parent::__sleep() ;
		
		$arrSavePropertys[] = Package::MakePrivatePropertyNameForSerialize('aORM',__CLASS__) ;
		
		return $arrSavePropertys ;
	}
	
	
	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @return	bool
	 */
	public function Load()
	{
		$this->GetORM()->Load($this) ;
		return true ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @param 	$aRecordSet		DBRecordSet
	 * @param 	$nTotalCount=0	int
	 * @return	void
	 */
	public function LoadFromRecordset(DBRecordSet $aRecordSet,$nTotalCount=0)
	{
		$aORM = $this->GetORM() ;
		
		while( $aRecordSet->GetRow() )
		{
			// create child model
			$aChildModel = $this->CreateChildModelByPrototype() ;
			
			// load child model
			$this->OnLoadChild($aRecordSet,$aORM,$aChildModel) ;
			
			//append
			$this->AddModel($aChildModel) ;
			
			$aRecordSet->Fetch() ;
		}
		
		// set total count
		$this->SetTotalCount($nTotalCount>0?$nTotalCount:$aRecordSet->GetCount()) ;
		
		// set Pagination 
		$aPagination = $this->GetPagination() ;
		if($aPagination and $nPageCount=$this->GetPageCount())
		{
			$aPagination->SetPageCount( $nPageCount ) ;
		}
		
		// reset inner iter
		$this->ResetInnerIterator() ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @param	$aRecordSet			DBRecordSet
	 * @param	$aORM				ORM
	 * @param	$aChildModel		IModel
	 * @return	void
	 */
	public function OnLoadChild(DBRecordSet $aRecordSet,ORM $aORM,IModel $aChildModel)
	{
		// load child model
		$aORM->LoadModelByRules($aChildModel,$aRecordSet) ;
		
		// set child model statue 
		$aChildModel->SetSerialized() ;
		$aChildModel->SetChanged(false) ;
	}

	public function Save( $bIgnoreChanged=false )
	{ ASSERT::ASSERT_(0,Language::SentenceEx('此模型为聚合模型，请对单项模型进行操作。','JCAT',null)) ; }
	
	public function Delete()
	{ ASSERT::ASSERT_(0,Language::SentenceEx('此模型为聚合模型，请对单项模型进行操作。','JCAT',null)) ; }
	
	public function Create()
	{ ASSERT::ASSERT_(0,Language::SentenceEx('此模型为聚合模型，请对单项模型进行操作。','JCAT',null)) ; }

	/**
	 * set a find by a property
	 *
	 * @access	public
	 * @param 	$sDataName			string
	 * @param 	$Value				mxied
	 * @param 	$sOperator='='		string
	 * @param 	$sRightB=null		mxied
	 * @return	void
	 */
	public function FindBy($sDataName,$Value,$sOperator='=',$sRightB=null)
	{
		$sFieldName = $this->GetORM()->GetFieldByProperty($sDataName) ;
		$this->arrQueryConditions[] = array($sFieldName,$Value,$sOperator,$sRightB) ;
	}
	
	/**
	 * set a find condition
	 *
	 * @access	public
	 * @param 	$sFieldName			string
	 * @param 	$Value				mxied
	 * @param 	$sOperator='='		string
	 * @param 	$sRightB=null		mxied
	 * @return	void
	 */
	public function FindByField($sFieldName,$Value,$sOperator='=',$sRightB=null)
	{
		$this->arrQueryConditions[] = array($sFieldName,$Value,$sOperator,$sRightB) ;
	}
	
	/**
	 * 设置关键词
	 *
	 * @access	public
	 * @param	$sKeywords			string
	 * @return	void
	 */
	public function FindByKeywords($sKeywords,$arrClmNames)
	{
		/*$aPrototype = $this->GetChildModelPrototype() ;
		$aWhere = $aPrototype->GetSubSQLWhere() ;
		$aFactory = $aWhere->GetFactory() ;
		$aGroup = $aWhere->AddGroup(false) ;

		foreach ($arrClmNames as $sClmName)
		{
			$aCondition = $aFactory->CreateSubSQLCondition(
								$aGroup
								, $aFactory->CreateColumn($sClmName)
								, "%{$sKeywords}%"
								, 'LIKE' ) ;
			$aGroup->AddCondition($aCondition) ;
		}*/
	}

	/**
	 * Description
	 *
	 * @access	public
	 * @param	$sDataName			string
	 * @param	$bDesc=true			bool
	 * @return	void
	 */
	public function FindSort($sDataName,$bDesc=true)
	{
		$this->GetORM()->AddOrderBy($sDataName,$bDesc);
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	IIterator
	 */
	public function CreateConditionIterator()
	{
		return new ArrayIterator($this->arrQueryConditions) ;
	}

	// 属性 ///////////////////////////////////////////////////////////////////////////////

	private $Prototype ;
	
	/**
	 * Description
	 * 
	 * @access	private
	 * @var		ORM
	 */
	private $aORM ;
	
	/**
	 * Description
	 * 
	 * @access	private
	 * @var		array
	 */
	private $arrQueryConditions = array() ;
}



?>